{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# python classes and Objects"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### python classes/objects\n",
    "> Almost everything in pythong is an object,with its properties and methods\n",
    "> A class is like an object constructor or a \"blueprint\" for creating objects\n",
    "\n",
    "#### create a class\n",
    "> To creat a class ,use the keyword class:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class '__main__.MyClass'>\n"
     ]
    }
   ],
   "source": [
    "class MyClass:\n",
    "    x = 5\n",
    "print(MyClass)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### creat Object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<__main__.MyClass object at 0x0000000004E66FD0>\n",
      "5\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'MyClass' object has no attribute 'y'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-6a4782ad77da>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mp1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mp1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mp1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'MyClass' object has no attribute 'y'"
     ]
    }
   ],
   "source": [
    "class MyClass:\n",
    "    x =5\n",
    "p1 = MyClass()\n",
    "print(p1)\n",
    "print(p1.x)\n",
    "print(p1.y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The __init__()function\n",
    "> The example above are classes and objects in their simplest form,and are not really useful in real life applications  \n",
    "> to undestand the meaning of classes we have to understand the built-in __init__()function  \n",
    "> all classes have a function called __init__() which is always executed when the class is being initiated  \n",
    "> Use the __init__() function to assign values to object properties ,or other operations that are necessary to do when the object is being created:  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "John\n",
      "36\n",
      "<__main__.Person object at 0x0000000004E91C50>\n"
     ]
    }
   ],
   "source": [
    "class Person:\n",
    "    def __init__(self,name,age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "p1 =Person(\"John\",36)\n",
    "print(p1.name)\n",
    "print(p1.age)\n",
    "print(p1)\n",
    "\n",
    "#NOTE:the __init__()function is called autonmatically every time the class is being used to create a new object"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Object Method\n",
    "> object can also contain methos ,Method in object are function that belong to the object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello my name is John\n"
     ]
    }
   ],
   "source": [
    "class Person:\n",
    "    def __init__(self,name,age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "    def myfunc(self):\n",
    "        print(\"Hello my name is \"+ self.name)\n",
    "p1 = Person(\"John\",36)\n",
    "p1.myfunc()\n",
    "# NOTE:the self parameter is a reference to the current instance of the class ,and is used to access variables the belong to the class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
